import {Order} from './order-model.js';
var order = new Order();
var app = getApp();
Page({
  data: {
    check:false
  },
  onLoad(options) {
    this.setData({
      orderid:options.id
    })
   this._loadData(options);
  },

  //添加地址
  address:function()
  {
    my.navigateTo({
      url:'/pages/addr/addr?aid=1'
    });
  },

  _loadData(options)
  {
     order.getMyDefaultAddress(app.globalData.uid,(data)=>{
       if(data.data)
       {
         this.addOrderAddress(data.data.id,options.id);
          this.setData({
         addressArr:data.data
       })
       }
    })

    //获取订单详情
    order.getOrderAndProductDetail(options.id,(data)=>{
      this.setData({
        detailArr:data.data
      })
    })
  },

  //将地址写入订单地址表中
  addOrderAddress(addressid,orderid)
  {
    order.addOrderAddress(addressid,orderid,(data)=>{

    })
  },
  onShow()
  {
    this.getAddress();
  },
  //获取选中的地址
  getAddress:function()
  {
    var that = this;
    var address = my.getStorageSync({key:'addressData'});
    if(address.data)
    {
       that.addOrderAddress(address.data.id,that.data.orderid);
           that.setData({
           addressArr:address.data
      });
      my.removeStorage({
        key: 'addressData', // 缓存数据的key
      });
    }
  },
  onChange:function(e)
  {
    if(e.detail.value[0] !=1)
    {
        this.setData({
          check:false
        })
    }else{
      this.setData({
          check:true
        })
    }
  },

  defaultTap:function()
  {
    var orderId = this.data.detailArr.id;
    var orderNo = this.data.detailArr.order_no
    var orderid = this.data.detailArr.id;
    if(!this.data.addressArr)
    {
     my.alert({
       title: '下单提示',
       content:"请选择地址",
       buttonText: '确定',
       success:function()
       {
         my.navigateTo({
           url:'/pages/addr/addr?aid=1'
         });
       } 
     });
     return;
    };

    if(!this.data.check)
    {
      my.alert({
        title: '下单提示',
         content:"请勾选协议",
      });
      return;
    }
    
      //获取预支付订单信息
      order.getPreOrder(orderId,orderNo,(data)=>{
          var out_trade_no = data.data.alipay_trade_create_response.out_trade_no;
          var trade_no = data.data.alipay_trade_create_response.trade_no;
          my.tradePay({
            tradeNO:trade_no,
            success: (res) => {
                if(res.resultCode ==9000)
                {
                  //更新订单状态生成分期账单
                  order.payNotify(orderid,(data)=>{
                    if(data.code ==1)
                    {
                      my.switchTab({
                        url: '/pages/user/user', 
                      });
                    }
                  })
                }else{
                  my.showToast({
                    type:'fail',
                    content:"支付失败",
                    success: (res) => {
                      my.switchTab({
                        url:"/pages/user/user"
                      })
                    },
                  });
                }
            },
          });
      })
  },

 
});
